{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-19T12:30:44.649910Z",
     "start_time": "2024-09-19T12:30:44.444571Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd \n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from  sklearn.metrics import  roc_auc_score, roc_curve ,accuracy_score,confusion_matrix,log_loss,auc,precision_recall_curve \n",
    "from sklearn.preprocessing import StandardScaler \n",
    "from sklearn.model_selection import train_test_split "
   ],
   "id": "8789626c3a40c52a",
   "outputs": [],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-19T12:30:45.484728Z",
     "start_time": "2024-09-19T12:30:44.650980Z"
    }
   },
   "source": [
    "#获取特征工程处理完的数据\n",
    "train_data = pd.read_csv(r\"D:\\桌面\\天猫复购预测\\data\\data_format1\\\\train_all_k.csv\")\n",
    "test_data = pd.read_csv(r\"D:\\桌面\\天猫复购预测\\data\\data_format1\\\\test_all_k.csv\")\n",
    "all_data = pd.read_csv(r\"D:\\桌面\\天猫复购预测\\data\\data_format1\\all_k.csv\")"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-19T12:30:45.489688Z",
     "start_time": "2024-09-19T12:30:45.485727Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def model_clf(model):\n",
    "    model.fit(X_train, y_train)  # 训练模型\n",
    "    y_train_pred = model.predict_proba(X_train)  # 预测训练集的概率\n",
    "    y_train_pred_pos = y_train_pred[:, 1]  # 获取正类的概率\n",
    "\n",
    "    y_test_pred = model.predict_proba(X_test)  # 预测测试集的概率\n",
    "    y_test_pred_pos = y_test_pred[:, 1]  # 获取正类的概率\n",
    "\n",
    "    auc_train = roc_auc_score(y_train, y_train_pred_pos)  # 计算训练集的 AUC 分数\n",
    "    auc_test = roc_auc_score(y_test, y_test_pred_pos)  # 计算测试集的 AUC 分数\n",
    "\n",
    "    print(f\"Train AUC Score {auc_train}\")  # 打印训练集的 AUC 分数\n",
    "    print(f\"Test AUC Score {auc_test}\")  # 打印测试集的 AUC 分数\n",
    "\n",
    "    fpr, tpr, _ = roc_curve(y_test, y_test_pred_pos)  # 绘制 ROC 曲线\n",
    "    return fpr, tpr  # 返回 FPR 和 TPR"
   ],
   "id": "26d03a20f3c187b2",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-19T12:30:45.497977Z",
     "start_time": "2024-09-19T12:30:45.490735Z"
    }
   },
   "cell_type": "code",
   "source": "train_data_1 = train_data ",
   "id": "f04910909a75cbdd",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-19T12:30:45.554624Z",
     "start_time": "2024-09-19T12:30:45.498977Z"
    }
   },
   "cell_type": "code",
   "source": "train_data_1",
   "id": "c6c2bb93a4b3a5e4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        user_id  merchant_id  label  prob  age_range  gender  sell_sum  \\\n",
       "0         34176         3906    0.0   0.0        6.0     0.0       451   \n",
       "1         34176          121    0.0   0.0        6.0     0.0       451   \n",
       "2         34176         4356    1.0   0.0        6.0     0.0       451   \n",
       "3         34176         2217    0.0   0.0        6.0     0.0       451   \n",
       "4        230784         4818    0.0   0.0        0.0     0.0        54   \n",
       "...         ...          ...    ...   ...        ...     ...       ...   \n",
       "257136   359807         4325    0.0   0.0        4.0     1.0       117   \n",
       "257137   294527         3971    0.0   0.0        0.0     1.0       198   \n",
       "257138   294527          152    0.0   0.0        0.0     1.0       198   \n",
       "257139   294527         2537    0.0   0.0        0.0     1.0       198   \n",
       "257140   229247         4140    0.0   0.0        4.0     2.0       194   \n",
       "\n",
       "        seller_id_unique  cat_id_unique  time_stamp_unique  ...  \\\n",
       "0                    109             45                 47  ...   \n",
       "1                    109             45                 47  ...   \n",
       "2                    109             45                 47  ...   \n",
       "3                    109             45                 47  ...   \n",
       "4                     20             17                 16  ...   \n",
       "...                  ...            ...                ...  ...   \n",
       "257136                33             25                 12  ...   \n",
       "257137                38             20                  6  ...   \n",
       "257138                38             20                  6  ...   \n",
       "257139                38             20                  6  ...   \n",
       "257140                50             29                 23  ...   \n",
       "\n",
       "        brand_id_most  action_type_most  seller_id_most_cnt  cat_id_most_cnt  \\\n",
       "0              4094.0                 0                  70               98   \n",
       "1              4094.0                 0                  70               98   \n",
       "2              4094.0                 0                  70               98   \n",
       "3              4094.0                 0                  70               98   \n",
       "4              1236.0                 0                  10                9   \n",
       "...               ...               ...                 ...              ...   \n",
       "257136         2276.0                 0                  22               15   \n",
       "257137         6143.0                 0                  28               38   \n",
       "257138         6143.0                 0                  28               38   \n",
       "257139         6143.0                 0                  28               38   \n",
       "257140         5697.0                 0                  24               33   \n",
       "\n",
       "        brand_id_most_cnt  action_type_most_cnt  action_type_sum_0  \\\n",
       "0                      70                   410                  0   \n",
       "1                      70                   410                  0   \n",
       "2                      70                   410                  0   \n",
       "3                      70                   410                  0   \n",
       "4                      10                    47                  0   \n",
       "...                   ...                   ...                ...   \n",
       "257136                 25                   107                  0   \n",
       "257137                 28                   162                  0   \n",
       "257138                 28                   162                  0   \n",
       "257139                 28                   162                  0   \n",
       "257140                 24                   181                  0   \n",
       "\n",
       "        action_type_sum_1  action_type_sum_2  action_type_sum_3  \n",
       "0                       0                  0                  0  \n",
       "1                       0                  0                  0  \n",
       "2                       0                  0                  0  \n",
       "3                       0                  0                  0  \n",
       "4                       0                  0                  0  \n",
       "...                   ...                ...                ...  \n",
       "257136                  0                  0                  0  \n",
       "257137                  0                  0                  0  \n",
       "257138                  0                  0                  0  \n",
       "257139                  0                  0                  0  \n",
       "257140                  0                  0                  0  \n",
       "\n",
       "[257141 rows x 24 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>prob</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "      <th>sell_sum</th>\n",
       "      <th>seller_id_unique</th>\n",
       "      <th>cat_id_unique</th>\n",
       "      <th>time_stamp_unique</th>\n",
       "      <th>...</th>\n",
       "      <th>brand_id_most</th>\n",
       "      <th>action_type_most</th>\n",
       "      <th>seller_id_most_cnt</th>\n",
       "      <th>cat_id_most_cnt</th>\n",
       "      <th>brand_id_most_cnt</th>\n",
       "      <th>action_type_most_cnt</th>\n",
       "      <th>action_type_sum_0</th>\n",
       "      <th>action_type_sum_1</th>\n",
       "      <th>action_type_sum_2</th>\n",
       "      <th>action_type_sum_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>34176</td>\n",
       "      <td>3906</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>451</td>\n",
       "      <td>109</td>\n",
       "      <td>45</td>\n",
       "      <td>47</td>\n",
       "      <td>...</td>\n",
       "      <td>4094.0</td>\n",
       "      <td>0</td>\n",
       "      <td>70</td>\n",
       "      <td>98</td>\n",
       "      <td>70</td>\n",
       "      <td>410</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34176</td>\n",
       "      <td>121</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>451</td>\n",
       "      <td>109</td>\n",
       "      <td>45</td>\n",
       "      <td>47</td>\n",
       "      <td>...</td>\n",
       "      <td>4094.0</td>\n",
       "      <td>0</td>\n",
       "      <td>70</td>\n",
       "      <td>98</td>\n",
       "      <td>70</td>\n",
       "      <td>410</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>34176</td>\n",
       "      <td>4356</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>451</td>\n",
       "      <td>109</td>\n",
       "      <td>45</td>\n",
       "      <td>47</td>\n",
       "      <td>...</td>\n",
       "      <td>4094.0</td>\n",
       "      <td>0</td>\n",
       "      <td>70</td>\n",
       "      <td>98</td>\n",
       "      <td>70</td>\n",
       "      <td>410</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34176</td>\n",
       "      <td>2217</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>451</td>\n",
       "      <td>109</td>\n",
       "      <td>45</td>\n",
       "      <td>47</td>\n",
       "      <td>...</td>\n",
       "      <td>4094.0</td>\n",
       "      <td>0</td>\n",
       "      <td>70</td>\n",
       "      <td>98</td>\n",
       "      <td>70</td>\n",
       "      <td>410</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>230784</td>\n",
       "      <td>4818</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>54</td>\n",
       "      <td>20</td>\n",
       "      <td>17</td>\n",
       "      <td>16</td>\n",
       "      <td>...</td>\n",
       "      <td>1236.0</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>47</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>257136</th>\n",
       "      <td>359807</td>\n",
       "      <td>4325</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>117</td>\n",
       "      <td>33</td>\n",
       "      <td>25</td>\n",
       "      <td>12</td>\n",
       "      <td>...</td>\n",
       "      <td>2276.0</td>\n",
       "      <td>0</td>\n",
       "      <td>22</td>\n",
       "      <td>15</td>\n",
       "      <td>25</td>\n",
       "      <td>107</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>257137</th>\n",
       "      <td>294527</td>\n",
       "      <td>3971</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>198</td>\n",
       "      <td>38</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>6143.0</td>\n",
       "      <td>0</td>\n",
       "      <td>28</td>\n",
       "      <td>38</td>\n",
       "      <td>28</td>\n",
       "      <td>162</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>257138</th>\n",
       "      <td>294527</td>\n",
       "      <td>152</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>198</td>\n",
       "      <td>38</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>6143.0</td>\n",
       "      <td>0</td>\n",
       "      <td>28</td>\n",
       "      <td>38</td>\n",
       "      <td>28</td>\n",
       "      <td>162</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>257139</th>\n",
       "      <td>294527</td>\n",
       "      <td>2537</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>198</td>\n",
       "      <td>38</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>6143.0</td>\n",
       "      <td>0</td>\n",
       "      <td>28</td>\n",
       "      <td>38</td>\n",
       "      <td>28</td>\n",
       "      <td>162</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>257140</th>\n",
       "      <td>229247</td>\n",
       "      <td>4140</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>194</td>\n",
       "      <td>50</td>\n",
       "      <td>29</td>\n",
       "      <td>23</td>\n",
       "      <td>...</td>\n",
       "      <td>5697.0</td>\n",
       "      <td>0</td>\n",
       "      <td>24</td>\n",
       "      <td>33</td>\n",
       "      <td>24</td>\n",
       "      <td>181</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>257141 rows × 24 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-19T12:30:45.617289Z",
     "start_time": "2024-09-19T12:30:45.555623Z"
    }
   },
   "cell_type": "code",
   "source": "train_data_1.fillna(0) #使用 0 填充train_data_1中的缺失值。",
   "id": "a4f76ddd925402fc",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        user_id  merchant_id  label  prob  age_range  gender  sell_sum  \\\n",
       "0         34176         3906    0.0   0.0        6.0     0.0       451   \n",
       "1         34176          121    0.0   0.0        6.0     0.0       451   \n",
       "2         34176         4356    1.0   0.0        6.0     0.0       451   \n",
       "3         34176         2217    0.0   0.0        6.0     0.0       451   \n",
       "4        230784         4818    0.0   0.0        0.0     0.0        54   \n",
       "...         ...          ...    ...   ...        ...     ...       ...   \n",
       "257136   359807         4325    0.0   0.0        4.0     1.0       117   \n",
       "257137   294527         3971    0.0   0.0        0.0     1.0       198   \n",
       "257138   294527          152    0.0   0.0        0.0     1.0       198   \n",
       "257139   294527         2537    0.0   0.0        0.0     1.0       198   \n",
       "257140   229247         4140    0.0   0.0        4.0     2.0       194   \n",
       "\n",
       "        seller_id_unique  cat_id_unique  time_stamp_unique  ...  \\\n",
       "0                    109             45                 47  ...   \n",
       "1                    109             45                 47  ...   \n",
       "2                    109             45                 47  ...   \n",
       "3                    109             45                 47  ...   \n",
       "4                     20             17                 16  ...   \n",
       "...                  ...            ...                ...  ...   \n",
       "257136                33             25                 12  ...   \n",
       "257137                38             20                  6  ...   \n",
       "257138                38             20                  6  ...   \n",
       "257139                38             20                  6  ...   \n",
       "257140                50             29                 23  ...   \n",
       "\n",
       "        brand_id_most  action_type_most  seller_id_most_cnt  cat_id_most_cnt  \\\n",
       "0              4094.0                 0                  70               98   \n",
       "1              4094.0                 0                  70               98   \n",
       "2              4094.0                 0                  70               98   \n",
       "3              4094.0                 0                  70               98   \n",
       "4              1236.0                 0                  10                9   \n",
       "...               ...               ...                 ...              ...   \n",
       "257136         2276.0                 0                  22               15   \n",
       "257137         6143.0                 0                  28               38   \n",
       "257138         6143.0                 0                  28               38   \n",
       "257139         6143.0                 0                  28               38   \n",
       "257140         5697.0                 0                  24               33   \n",
       "\n",
       "        brand_id_most_cnt  action_type_most_cnt  action_type_sum_0  \\\n",
       "0                      70                   410                  0   \n",
       "1                      70                   410                  0   \n",
       "2                      70                   410                  0   \n",
       "3                      70                   410                  0   \n",
       "4                      10                    47                  0   \n",
       "...                   ...                   ...                ...   \n",
       "257136                 25                   107                  0   \n",
       "257137                 28                   162                  0   \n",
       "257138                 28                   162                  0   \n",
       "257139                 28                   162                  0   \n",
       "257140                 24                   181                  0   \n",
       "\n",
       "        action_type_sum_1  action_type_sum_2  action_type_sum_3  \n",
       "0                       0                  0                  0  \n",
       "1                       0                  0                  0  \n",
       "2                       0                  0                  0  \n",
       "3                       0                  0                  0  \n",
       "4                       0                  0                  0  \n",
       "...                   ...                ...                ...  \n",
       "257136                  0                  0                  0  \n",
       "257137                  0                  0                  0  \n",
       "257138                  0                  0                  0  \n",
       "257139                  0                  0                  0  \n",
       "257140                  0                  0                  0  \n",
       "\n",
       "[257141 rows x 24 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>prob</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "      <th>sell_sum</th>\n",
       "      <th>seller_id_unique</th>\n",
       "      <th>cat_id_unique</th>\n",
       "      <th>time_stamp_unique</th>\n",
       "      <th>...</th>\n",
       "      <th>brand_id_most</th>\n",
       "      <th>action_type_most</th>\n",
       "      <th>seller_id_most_cnt</th>\n",
       "      <th>cat_id_most_cnt</th>\n",
       "      <th>brand_id_most_cnt</th>\n",
       "      <th>action_type_most_cnt</th>\n",
       "      <th>action_type_sum_0</th>\n",
       "      <th>action_type_sum_1</th>\n",
       "      <th>action_type_sum_2</th>\n",
       "      <th>action_type_sum_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>34176</td>\n",
       "      <td>3906</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>451</td>\n",
       "      <td>109</td>\n",
       "      <td>45</td>\n",
       "      <td>47</td>\n",
       "      <td>...</td>\n",
       "      <td>4094.0</td>\n",
       "      <td>0</td>\n",
       "      <td>70</td>\n",
       "      <td>98</td>\n",
       "      <td>70</td>\n",
       "      <td>410</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34176</td>\n",
       "      <td>121</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>451</td>\n",
       "      <td>109</td>\n",
       "      <td>45</td>\n",
       "      <td>47</td>\n",
       "      <td>...</td>\n",
       "      <td>4094.0</td>\n",
       "      <td>0</td>\n",
       "      <td>70</td>\n",
       "      <td>98</td>\n",
       "      <td>70</td>\n",
       "      <td>410</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>34176</td>\n",
       "      <td>4356</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>451</td>\n",
       "      <td>109</td>\n",
       "      <td>45</td>\n",
       "      <td>47</td>\n",
       "      <td>...</td>\n",
       "      <td>4094.0</td>\n",
       "      <td>0</td>\n",
       "      <td>70</td>\n",
       "      <td>98</td>\n",
       "      <td>70</td>\n",
       "      <td>410</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34176</td>\n",
       "      <td>2217</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>451</td>\n",
       "      <td>109</td>\n",
       "      <td>45</td>\n",
       "      <td>47</td>\n",
       "      <td>...</td>\n",
       "      <td>4094.0</td>\n",
       "      <td>0</td>\n",
       "      <td>70</td>\n",
       "      <td>98</td>\n",
       "      <td>70</td>\n",
       "      <td>410</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>230784</td>\n",
       "      <td>4818</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>54</td>\n",
       "      <td>20</td>\n",
       "      <td>17</td>\n",
       "      <td>16</td>\n",
       "      <td>...</td>\n",
       "      <td>1236.0</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>47</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>257136</th>\n",
       "      <td>359807</td>\n",
       "      <td>4325</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>117</td>\n",
       "      <td>33</td>\n",
       "      <td>25</td>\n",
       "      <td>12</td>\n",
       "      <td>...</td>\n",
       "      <td>2276.0</td>\n",
       "      <td>0</td>\n",
       "      <td>22</td>\n",
       "      <td>15</td>\n",
       "      <td>25</td>\n",
       "      <td>107</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>257137</th>\n",
       "      <td>294527</td>\n",
       "      <td>3971</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>198</td>\n",
       "      <td>38</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>6143.0</td>\n",
       "      <td>0</td>\n",
       "      <td>28</td>\n",
       "      <td>38</td>\n",
       "      <td>28</td>\n",
       "      <td>162</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>257138</th>\n",
       "      <td>294527</td>\n",
       "      <td>152</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>198</td>\n",
       "      <td>38</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>6143.0</td>\n",
       "      <td>0</td>\n",
       "      <td>28</td>\n",
       "      <td>38</td>\n",
       "      <td>28</td>\n",
       "      <td>162</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>257139</th>\n",
       "      <td>294527</td>\n",
       "      <td>2537</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>198</td>\n",
       "      <td>38</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>6143.0</td>\n",
       "      <td>0</td>\n",
       "      <td>28</td>\n",
       "      <td>38</td>\n",
       "      <td>28</td>\n",
       "      <td>162</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>257140</th>\n",
       "      <td>229247</td>\n",
       "      <td>4140</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>194</td>\n",
       "      <td>50</td>\n",
       "      <td>29</td>\n",
       "      <td>23</td>\n",
       "      <td>...</td>\n",
       "      <td>5697.0</td>\n",
       "      <td>0</td>\n",
       "      <td>24</td>\n",
       "      <td>33</td>\n",
       "      <td>24</td>\n",
       "      <td>181</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>257141 rows × 24 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-19T12:30:45.621050Z",
     "start_time": "2024-09-19T12:30:45.618290Z"
    }
   },
   "cell_type": "code",
   "source": [
    "label = train_data_1['label']#从train_data_1中提取标签列。\n",
    "del train_data_1['user_id']\n",
    "del train_data_1['merchant_id']\n",
    "del train_data_1['label']"
   ],
   "id": "38324d479e4550e8",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-19T12:30:45.734944Z",
     "start_time": "2024-09-19T12:30:45.622050Z"
    }
   },
   "cell_type": "code",
   "source": [
    "stdScaler = StandardScaler()#创建一个StandardScaler对象，用于标准化数据。\n",
    "X = stdScaler.fit_transform(train_data_1)#使用StandardScaler对象对train_data_1进行标准化处理。 "
   ],
   "id": "a138d8c3325aa652",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-19T12:30:46.428565Z",
     "start_time": "2024-09-19T12:30:45.735946Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, label, test_size=0.2, random_state=9)#将数据集分为训练集和测试集，其中测试集占 20%，随机种子为 9。\n",
    "clf = LogisticRegression(random_state=0, solver='lbfgs', multi_class='multinomial', class_weight='balanced')#创建一个逻辑回归模型，设置随机种子为 0，求解器为lbfgs，多分类为multinomial，类别权重为balanced。\n",
    "model_clf(clf)#使用clf模型进行训练。"
   ],
   "id": "fcbf5d99989b9e73",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda3\\Lib\\site-packages\\sklearn\\linear_model\\_logistic.py:1237: FutureWarning: 'multi_class' was deprecated in version 1.5 and will be removed in 1.7. From then on, binary problems will be fit as proper binary  logistic regression models (as if multi_class='ovr' were set). Leave it to its default value to avoid this warning.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train AUC Score 0.5788004057815783\n",
      "Test AUC Score 0.5750996829973776\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(array([0.00000000e+00, 2.07326934e-05, 2.07326934e-04, ...,\n",
       "        9.99419485e-01, 9.99958535e-01, 1.00000000e+00]),\n",
       " array([0., 0., 0., ..., 1., 1., 1.]))"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "db3113f58fb24e36"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
